package com.ctshk.rpc.booking.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.common.utils.SnowflakeIdWorker;
import com.ctshk.rpc.booking.dto.outside.car.query.SupplierOutsideOrderCarRentalDTO;
import com.ctshk.rpc.booking.dto.outside.car.query.SupplierOutsideOrderCarRentalTouristDTO;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderCarRental;
import com.ctshk.rpc.booking.entity.SupplierOutsideOrderCarRentalTourist;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderCarRentalMapper;
import com.ctshk.rpc.booking.mapper.SupplierOutsideOrderCarRentalTouristMapper;
import com.ctshk.rpc.booking.req.outside.car.save.SupplierOutsideOrderCarRentalReq;
import com.ctshk.rpc.booking.service.ISupplierOutsideOrderCarRentalService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 站外租车出行信息 服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-03-09
 */
@DubboService
public class SupplierOutsideOrderCarRentalServiceImpl implements ISupplierOutsideOrderCarRentalService {
    @Autowired
    SupplierOutsideOrderCarRentalMapper supplierOutsideOrderCarRentalMapper;
    @Autowired
    SupplierOutsideOrderCarRentalTouristMapper supplierOutsideOrderCarRentalTouristMapper;

    @Override
    public void save(Long supplierOrderId, Long userId, LocalDateTime nowDateTime, List<SupplierOutsideOrderCarRentalReq> supplierOutsideOrderCarRentalReqList) {
        if (supplierOrderId != null) {
            //删除
            QueryWrapper<SupplierOutsideOrderCarRental> outsideOrderCarRentalWrapper = new QueryWrapper<>();
            outsideOrderCarRentalWrapper.lambda().eq(SupplierOutsideOrderCarRental::getOrderId, supplierOrderId);
            supplierOutsideOrderCarRentalMapper.delete(outsideOrderCarRentalWrapper);
            QueryWrapper<SupplierOutsideOrderCarRentalTourist> outsideOrderCarRentalTouristWrapper = new QueryWrapper<>();
            outsideOrderCarRentalTouristWrapper.lambda().eq(SupplierOutsideOrderCarRentalTourist::getOrderId, supplierOrderId);
            supplierOutsideOrderCarRentalTouristMapper.delete(outsideOrderCarRentalTouristWrapper);
        }
        //新增出租车信息
        supplierOutsideOrderCarRentalReqList.forEach(outsideOrderCarRentalReq -> {
            SupplierOutsideOrderCarRental supplierOutsideOrderCarRental = EntityUtil.copy(outsideOrderCarRentalReq, SupplierOutsideOrderCarRental.class);
            supplierOutsideOrderCarRental.setId(SnowflakeIdWorker.nextId());
            supplierOutsideOrderCarRental.setOrderId(supplierOrderId);
            supplierOutsideOrderCarRental.setCreateId(userId);
            supplierOutsideOrderCarRental.setGmtCreate(nowDateTime);
            supplierOutsideOrderCarRental.setModifiedId(userId);
            supplierOutsideOrderCarRental.setGmtModified(nowDateTime);
            supplierOutsideOrderCarRental.setIsDeleted(IsDeletedCode.NO.getCode());
            supplierOutsideOrderCarRentalMapper.insert(supplierOutsideOrderCarRental);
            if (CollectionUtils.isNotEmpty(outsideOrderCarRentalReq.getSupplierOutsideOrderCarRentalTouristReqList())) {
                //新增出租车旅客信息
                outsideOrderCarRentalReq.getSupplierOutsideOrderCarRentalTouristReqList().forEach(outsideOrderCarRentalTouristReq -> {
                    SupplierOutsideOrderCarRentalTourist supplierOutsideOrderCarRentalTourist = EntityUtil.copy(outsideOrderCarRentalTouristReq, SupplierOutsideOrderCarRentalTourist.class);
                    supplierOutsideOrderCarRentalTourist.setId(SnowflakeIdWorker.nextId());
                    supplierOutsideOrderCarRentalTourist.setOrderId(supplierOrderId);
                    supplierOutsideOrderCarRentalTourist.setCarId(supplierOutsideOrderCarRental.getId());
                    supplierOutsideOrderCarRentalTourist.setCreateId(userId);
                    supplierOutsideOrderCarRentalTourist.setGmtCreate(nowDateTime);
                    supplierOutsideOrderCarRentalTourist.setModifiedId(userId);
                    supplierOutsideOrderCarRentalTourist.setGmtModified(nowDateTime);
                    supplierOutsideOrderCarRentalTourist.setIsDeleted(IsDeletedCode.NO.getCode());
                    supplierOutsideOrderCarRentalTouristMapper.insert(supplierOutsideOrderCarRentalTourist);
                });
            }
        });
    }

    @Override
    public List<SupplierOutsideOrderCarRentalDTO> selectByOrderId(Long id) {
        List<SupplierOutsideOrderCarRentalDTO> supplierOutsideOrderCarRentalDTOS = new ArrayList<>();
        //查询机票列表
        QueryWrapper<SupplierOutsideOrderCarRental> carWrapper = new QueryWrapper();
        carWrapper.lambda().eq(SupplierOutsideOrderCarRental::getOrderId, id);
        List<SupplierOutsideOrderCarRental> supplierOutsideOrderCarRentals = supplierOutsideOrderCarRentalMapper.selectList(carWrapper);
        if (CollectionUtils.isNotEmpty(supplierOutsideOrderCarRentals)) {
            supplierOutsideOrderCarRentalDTOS = EntityUtil.copyList(supplierOutsideOrderCarRentals, SupplierOutsideOrderCarRentalDTO.class);
            supplierOutsideOrderCarRentalDTOS.forEach(car -> {
                //查询出租车旅客列表
                QueryWrapper<SupplierOutsideOrderCarRentalTourist> carTouristWrapper = new QueryWrapper();
                carTouristWrapper.lambda().eq(SupplierOutsideOrderCarRentalTourist::getOrderId, id);
                carTouristWrapper.lambda().eq(SupplierOutsideOrderCarRentalTourist::getCarId, car.getId());
                List<SupplierOutsideOrderCarRentalTourist> supplierOutsideOrderCarRentalTourists = supplierOutsideOrderCarRentalTouristMapper.selectList(carTouristWrapper);
                if (CollectionUtils.isNotEmpty(supplierOutsideOrderCarRentalTourists)) {
                    List<SupplierOutsideOrderCarRentalTouristDTO> supplierOutsideOrderCarRentalTouristDTOList = EntityUtil.copyList(supplierOutsideOrderCarRentalTourists, SupplierOutsideOrderCarRentalTouristDTO.class);
                    car.setSupplierOutsideOrderCarRentalTouristDTOList(supplierOutsideOrderCarRentalTouristDTOList);
                }
            });
        }
        return supplierOutsideOrderCarRentalDTOS;
    }
}
